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

Error: Cannot retrieve vector from IRValue format int32 #2428

Open
anhnd101 opened this issue Jan 7, 2025 · 1 comment
Open

Error: Cannot retrieve vector from IRValue format int32 #2428

anhnd101 opened this issue Jan 7, 2025 · 1 comment
Labels
bug Unexpected behaviour that should be corrected (type)

Comments

@anhnd101
Copy link

anhnd101 commented Jan 7, 2025

🐞Describing the bug

I convert model to Coreml, model use this method:

def create_2d_sin_embedding(d_model, height, width, device="cpu", max_period=10000):
    """
    :param d_model: dimension of the model
    :param height: height of the positions
    :param width: width of the positions
    :return: d_model*height*width position matrix
    """
    if d_model % 4 != 0:
        raise ValueError("Cannot use sin/cos positional encoding with " "odd dimension (got dim={:d})".format(d_model))
    pe = torch.zeros(d_model, height, width)
    # Each dimension use half of d_model
    d_model = int(d_model / 2)
    div_term = torch.exp(torch.arange(0.0, d_model, 2) * -(math.log(max_period) / d_model))
    pos_w = torch.arange(0.0, width).unsqueeze(1)
    pos_h = torch.arange(0.0, height).unsqueeze(1)
    pe[0:d_model:2, :, :] = torch.sin(pos_w * div_term).transpose(0, 1).unsqueeze(1).repeat(1, height, 1)
    pe[1:d_model:2, :, :] = torch.cos(pos_w * div_term).transpose(0, 1).unsqueeze(1).repeat(1, height, 1)
    pe[d_model::2, :, :] = torch.sin(pos_h * div_term).transpose(0, 1).unsqueeze(2).repeat(1, 1, width)
    pe[d_model + 1 :: 2, :, :] = torch.cos(pos_h * div_term).transpose(0, 1).unsqueeze(2).repeat(1, 1, width)
    return pe[None, :].to(device)

It converted successful, but then I do the Init the model in XCode, it printed this error: Error: Cannot retrieve vector from IRValue format int32
I think it because this slice: [0:d_model:2, :, :],
Can someone help fix this ?

Paste Python code snippet here, complete with any required import statements.

- If the model conversion succeeds, but there is a numerical mismatch in predictions, please include the code used for comparisons.

## System environment (please complete the following information):
 - coremltools version: 8.1
 - MacOS

@anhnd101 anhnd101 added the bug Unexpected behaviour that should be corrected (type) label Jan 7, 2025
@YifanShenSZ
Copy link
Collaborator

Hi @anhnd101 could you please make your reproduce more complete?

Concretely, what you shared in PR description seems to be only a function coded in torch. Could you please share the entire export -> convert script, i.e. something like

import torch

import coremltools as ct


class Model(torch.nn.Module):
    def forward(self, ...):
        ...

model = Model()
model.eval()

example_inputs = ...
exported_program = torch.export.export(model, example_inputs)

mlmodel = ct.convert(exported_program)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Unexpected behaviour that should be corrected (type)
Projects
None yet
Development

No branches or pull requests

2 participants