GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Error on subindice Embedding type for Torch Tensor moving from GPU to CPU. · Issue #1830 · docarray/docarray

This repository was archived by the owner on Sep 9, 2026. It is now read-only.
This repository was archived by the owner on Sep 9, 2026. It is now read-only.

Error on subindice Embedding type for Torch Tensor moving from GPU to CPU. #1830

Copy link
Copy link
Open
Open
Error on subindice Embedding type for Torch Tensor moving from GPU to CPU.#1830
Copy link

Description

@vincetrep
vincetrep
opened on Nov 6, 2023
Issue body actions

Initial Checks

  • I have read and followed the docs and still think this is a bug

Description

I have found an issue with the following sequence:

Object
SubIndiceObject with an attribute Optional[AnyEmbedding]

I am running an operation on GPU and storing the TorchEmbedding on that attribute

Then I am converting the value back into a NDArrayEmbedding on CPU and storing in the same attribute.

When indexing the data into weaviate, it still tracks that attribute as a TorchTensor on GPU and tries to convert to a NDArrayEmbedding.

In order to fix it, I ran the cpu() operation prior to saving that tensor on the attribute.

I don't know if it is a bug or by design but logging the scenario nonetheless.

Example Code

No response

Python, DocArray & OS Version

docarray version:  0.39.1

Affected Components

  • Vector Database / Index
  • Representing
  • Sending
  • storing
  • multi modal data type

Activity

  1. JoanFM commented on Nov 6, 2023

    @JoanFM
    JoanFM
    on Nov 6, 2023
    Member
    More actions

    Hey @vincetrep ,

    do u have a minimal reproducible code that we can use to debug?

  2. JohannesMessner commented on Nov 8, 2023

    @JohannesMessner
    JohannesMessner
    on Nov 8, 2023
    Member
    More actions

    I also cannot reproduce the issue, the following executes without problem:

    from docarray.index.backends.weaviate import EmbeddedOptions
    from docarray.index.backends.weaviate import WeaviateDocumentIndex
    from typing import Optional
    from docarray.typing import AnyEmbedding, NdArrayEmbedding
    from pydantic import Field, parse_obj_as
    from docarray import DocList, BaseDoc
    import numpy as np
    import torch
    
    class SubDoc(BaseDoc):
        embedding: Optional[AnyEmbedding] = Field(is_embedding=True, n_dim=128)
    
    
    class MyDoc(BaseDoc):
        text: str 
        subdocs: DocList[SubDoc]
    
    
    dbconfig = WeaviateDocumentIndex.DBConfig(embedded_options=EmbeddedOptions())
    doc_index = WeaviateDocumentIndex[MyDoc](db_config=dbconfig)
    
    # creating tensors on GPU
    gpu_tensors = [torch.rand(128).to('cuda:2') for i in range(3)]
    assert all(t.is_cuda for t in gpu_tensors)
    # storing them in DocList
    data = DocList[MyDoc](
        [
            MyDoc(text='hello world', subdocs=[SubDoc(embedding=t)]) for t in gpu_tensors
        ]
    )
    # converting to NdArray on CPU
    for doc in data:
        doc.subdocs[0].embedding = parse_obj_as(NdArrayEmbedding, doc.subdocs[0].embedding)
        assert isinstance(doc.subdocs[0].embedding, np.ndarray)  # not on GPU
    # insert into index
    doc_index.index(data)
    
    root_docs, sub_docs, scores = doc_index.find_subindex(np.random.rand(128), subindex='subdocs', limit=3)
    
    print(scores)
  3. Sumant02 commented on Dec 23, 2023

    @Sumant02
    Sumant02
    on Dec 23, 2023
    More actions

    `from docarray.index.backends.weaviate import WeaviateDocumentIndex
    doc_index = WeaviateDocumentIndexMyDoc

    from docarray.index.backends.weaviate import EmbeddedOptions
    from docarray.index.backends.weaviate import WeaviateDocumentIndex
    from typing import Optional
    from docarray.typing import AnyEmbedding, NdArrayEmbedding
    from pydantic import Field, parse_obj_as
    from docarray import DocList, BaseDoc
    import numpy as np
    import torch

    class SubDoc(BaseDoc):
    embedding: Optional[AnyEmbedding] = Field(is_embedding=True, n_dim=128)

    class MyDoc(BaseDoc):
    text: str
    subdocs: DocList[SubDoc]

    dbconfig = WeaviateDocumentIndex.DBConfig(embedded_options=EmbeddedOptions())
    doc_index = WeaviateDocumentIndexMyDoc

    creating tensors on GPU

    gpu_tensors = [torch.rand(128).to('cuda:2') for i in range(3)]
    assert all(t.is_cuda for t in gpu_tensors)

    storing them in DocList

    data = DocList[MyDoc](
    [
    MyDoc(text='hello world', subdocs=[SubDoc(embedding=t)]) for t in gpu_tensors
    ]
    )

    converting to NdArray on CPU

    for doc in data:
    doc.subdocs[0].embedding = parse_obj_as(NdArrayEmbedding, doc.subdocs[0].embedding)
    assert isinstance(doc.subdocs[0].embedding, np.ndarray) # not on GPU

    insert into index

    doc_index.index(data)

    root_docs, sub_docs, scores = doc_index.find_subindex(np.random.rand(128), subindex='subdocs', limit=3)
    print(scores)`
    @JohannesMessner JohannesMessner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.