NotImplementedError: Cannot copy out of meta tensor; no data!

#3
by terrylimax - opened

What could be the issue?

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
/tmp/ipython-input-2304534987.py in <cell line: 0>()
      7 device = "cuda" if torch.cuda.is_available() else "cpu"
      8 
----> 9 model = M2M100ForConditionalGeneration.from_pretrained(path_to_model).to(device)
     10 
     11 tokenizer = M2M100Tokenizer.from_pretrained(path_to_model)
...
/usr/local/lib/python3.12/dist-packages/torch/nn/modules/module.py in _apply(self, fn, recurse)
    953             # `with torch.no_grad():`
    954             with torch.no_grad():
--> 955                 param_applied = fn(param)
    956             p_should_use_set_data = compute_should_use_set_data(param, param_applied)
    957 
/usr/local/lib/python3.12/dist-packages/torch/nn/modules/module.py in convert(t)
   1360             except NotImplementedError as e:
   1361                 if str(e) == "Cannot copy out of meta tensor; no data!":
-> 1362                     raise NotImplementedError(
   1363                         f"{e} Please use torch.nn.Module.to_empty() instead of torch.nn.Module.to() "
   1364                         f"when moving module from meta to a different device."
NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty() instead of torch.nn.Module.to() when moving module from meta to a different device.

https://github.com/kijai/ComfyUI-FluxTrainer/issues/80 here it says encoder files might be corruptive

import torch
from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
from tqdm import tqdm

path_to_model = "ai-forever/RuM2M100-418M"

device = "cuda" if torch.cuda.is_available() else "cpu"

model = M2M100ForConditionalGeneration.from_pretrained(path_to_model).to(device)

tokenizer = M2M100Tokenizer.from_pretrained(path_to_model)
tokenizer.src_lang = "ru"
tokenizer.tgt_lang = "ru"

batch_size = 16
decoded_all = []

MAX_NEW_TOKENS = 96

sequences = input_sequences 

with torch.inference_mode():
    for i in tqdm(range(0, len(sequences), batch_size),
                  total=(len(sequences) + batch_size - 1) // batch_size,
                  desc="Batches"):
        batch = sequences[i:i + batch_size]

        encoded = tokenizer(
            batch,
            padding="longest",
            truncation=False,
            return_tensors="pt",
            return_attention_mask=True
        )

        encoded = {k: v.to(device) for k, v in encoded.items()}

        predicts = model.generate(
            **encoded,
            forced_bos_token_id=tokenizer.get_lang_id("ru"),
            max_new_tokens=MAX_NEW_TOKENS,
            use_cache=True
        )

        decoded = tokenizer.batch_decode(predicts, skip_special_tokens=True)
        decoded_all.extend([d.strip() for d in decoded])

        del encoded, predicts
        if device == "cuda":
          torch.cuda.empty_cache()

RuM2M100_418M_spell_correction = decoded_all

Sign up or log in to comment