OpenNMT / OpenNMT/CTranslate2

Not able to convert the last_hidden_state values when using a bf16 converted model weights

Open
#1,910 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
4.7k
Forks
536
Avg merge
12h 12m
Merged PRs (30d)
4

Description

I have a encoder module, which i have converted to ctranslate in the following precision

  1. fp32
  2. fp16
  3. bf16
  4. int8

and i try to run the following code:

import torch.nn.functional
import ctranslate2
import numpy as np
import torch
import transformers

from sentence_transformers import SentenceTransformer


def ctranslate_infer(model_path, tokenizer_name):
    device = "cuda"
    encoder = ctranslate2.Encoder(model_path, device=device)


    tokenizer = transformers.AutoTokenizer.from_pretrained(tokenizer_name)
    inputs = ["It was good!"]
    tokens = tokenizer(inputs).input_ids

    output = encoder.forward_batch(tokens)
    # for out in output:
    
    embeddings = torch.as_tensor(output.last_hidden_state, device=device)
    embeddings = embeddings[0][0]
    print(embeddings.shape)
    embeddings = torch.nn.functional.normalize(embeddings.view(1,-1), p=2.0)
    print(embeddings)

def st_infer(model_path):
    inputs = ["It was good!"]
    st_model = SentenceTransformer(model_path)
    print(st_model)
    print(st_model.encode(inputs))


if __name__ == "__main__":
    ctranslate_infer("bge_m3_ctranslate", "BAAI/bge-m3")
    print("-----------------------------------------------------------------------------")
    # st_infer("BAAI/bge-m3")

    ctranslate_infer("bge_m3_fp16_ctranslate", "BAAI/bge-m3")
    print("-----------------------------------------------------------------------------")
    
    ctranslate_infer("bge_m3_bf16_ctranslate", "BAAI/bge-m3")
    print("-----------------------------------------------------------------------------")

    ctranslate_infer("bge_m3_int8_ctranslate", "BAAI/bge-m3")
    print("-----------------------------------------------------------------------------")

But when i try to infer from the bf16 model i get the following error,

torch.Size([1024])
tensor([[ 0.0102,  0.0150, -0.0657,  ..., -0.0160, -0.0231,  0.0077]],
       device='cuda:0')
-----------------------------------------------------------------------------
torch.Size([1024])
tensor([[ 0.0102,  0.0151, -0.0657,  ..., -0.0160, -0.0232,  0.0077]],
       device='cuda:0', dtype=torch.float16)
-----------------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rkoy/vectorizerstats/ctranslate/infer.py", line 43, in <module>
    ctranslate_infer("bge_m3_bf16_ctranslate", "BAAI/bge-m3")
  File "/data/rkoy/vectorizerstats/ctranslate/infer.py", line 22, in ctranslate_infer
    embeddings = torch.as_tensor(output.last_hidden_state, device=device)
RuntimeError: Could not infer dtype of ctranslate2._ext.StorageView

Ctranslate version: 4.6.0
pytorch version: 2.7.1
GPU: L40S

I would like to know whether am i missing anything or is it a bug in the library ?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure from infer.py using ctranslate2.Encoder.forward_batch and the bf16 model, then inspect how output.last_hidden_state is exposed to torch.as_tensor. Compare the fp32 and fp16 paths with bf16 handling; done when the behavior is fixed or the unsupported conversion is clearly documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python, pytorch
Domain
backend, machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.