AttributeError: module transformers has no attribute RWForCausalLM / MosaicGPT  / GPT2LMHeadCustomModel / BTLMLMHeadModel / MossForCausalLM / MPTForCausalLM / InternLMForCausalLM / DistilBertJapaneseTokenizer / FalconForCausalLM / LongLlamaForCausalLM
Author: amarahiqbal
Pull request: no

### System Info

Python Version: 3.10.11 (main, May 16 2023, 00:28:57) [GCC 11.2.0]
Transformers Version: 4.29.2
Platform: Azure

### Who can help?

@gante  @Rocketknight1 @ArthurZucker  @younesbelkada

### Information

- [ ] The official example scripts
- [X] My own modified scripts

### Tasks

- [X] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [ ] My own task or dataset (give details below)

### Reproduction

loaded_model_pipeline = mlflow.transformers.load_model(model_uri=model_detail.source, return_type="pipeline")
File /anaconda/envs/azureml_py310_sdkv2/lib/python3.10/site-packages/transformers/utils/import_utils.py:1165, in _LazyModule.__getattr__(self, name)
   1163     value = getattr(module, name)
   1164 else:
-> 1165     raise AttributeError(f"module {self.__name__} has no attribute {name}")
   1167 setattr(self, name, value)
   1168 return value

AttributeError: module transformers has no attribute BaiChuanForCausalLM

### Expected behavior

Am bringing the model from hugging face and registering it in the Azure workspace. But when am trying to load the model back, it is throwing me  AttributeError.

Comments:
Hey @amarahiqbal, I'm not sure we have access to your entire script nor traceback. Could you please share it here so that we may see how to help you best?

Thanks.

Sharing the script for 1 Attribute Error - 

```py
from huggingface_hub import HfApi
import pandas as pd
import re

LIST_OF_COLUMNS = ['modelId', 'downloads',
                   'lastModified', 'tags', 'pipeline_tag']
TASK_NAME = ['fill-mask', 'token-classification', 'question-answering',
             'summarization', 'text-generation', 'text-classification', 'translation']
STRING_TO_CHECK = 'transformers'
model_name = "cerebras/btlm-3b-8k-base"

hf_api = HfApi()
models = hf_api.list_models(
    full=True, sort='lastModified', direction=-1)

required_data = [i for i in models]

daata_dict = {}

for data in required_data:
    for key in data.__dict__.keys():
        if key in LIST_OF_COLUMNS:
            if daata_dict.get(key) is None:
                daata_dict[key] = []
            values = daata_dict.get(key)
            if key == 'tags':
                values.append(data.__dict__.get(key, ["Empty"]))
            else:
                values.append(data.__dict__.get(key, "Empty"))
            daata_dict[key] = values

df = pd.DataFrame(daata_dict)
df = df[df.tags.apply(lambda x: STRING_TO_CHECK in x)]
df = df[df['pipeline_tag'].isin(TASK_NAME)]

required_data = df[df.modelId.apply(lambda x: x == model_name)]
task = required_data["pipeline_tag"].to_string()
pattern = r'[0-9\s+]'
task = re.sub(pattern, '', task)

registered_model_name = model_name.replace("/", "-")
artifact_path = registered_model_name + "-artifact"
library_name = library.get(task)
model_library = getattr(transformers, library_name)
model = model_library.from_pretrained(model_name, trust_remote_code=True)

from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
from transformers import pipeline
model_and_tokenizer = {"model": model, "tokenizer": tokenizer}

pipeline = pipeline(task=task, model=model, tokenizer=tokenizer)

output = generate_signature_output(
    pipeline, text_generation.input_data)
signature = infer_signature(text_generation.input_data, output)
mlflow.transformers.log_model(
    transformers_model=pipeline,
    task=task,
    artifact_path=artifact_path,
    registered_model_name=registered_model_name,
    signature=signature,
    input_example=text_generation.input_data
)

from mlflow.tracking.client import MlflowClient
client = MlflowClient()
registered_model_detail = client.get_latest_versions(
    name=registered_model_name, stages=["None"])
model_detail = registered_model_detail[0]
print("Latest registered model version is : ", model_detail.version)
loaded_model_pipeline = mlflow.transformers.load_model(
    model_uri=model_detail.source, return_type="pipeline")
```

Got below Error - 
```
File /anaconda/envs/azureml_py310_sdkv2/lib/python3.10/site-packages/transformers/utils/import_utils.py:1165, in _LazyModule.__getattr__(self, name)
   1163     value = getattr(module, name)
   1164 else:
-> 1165     raise AttributeError(f"module {self.__name__} has no attribute {name}")
   1167 setattr(self, name, value)
   1168 return value

AttributeError: module transformers has no attribute BTLMLMHeadModel
```

Is that the full stack trace? It seems like there are a few tracebacks missing

Its the entire end to end script. The error is coming on the last line when am loading back the model.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[24], line 85
     83 model_detail = registered_model_detail[0]
     84 print("Latest registered model version is : ", model_detail.version)
---> 85 loaded_model_pipeline = mlflow.transformers.load_model(model_uri=model_detail.source, return_type="pipeline")

File /anaconda/envs/azureml_py310_sdkv2/lib/python3.10/site-packages/mlflow/utils/docstring_utils.py:235, in docstring_version_compatibility_warning.<locals>.annotated_func.<locals>.version_func(*args, **kwargs)
    233 if installed_version < Version(min_ver) or installed_version > Version(max_ver):
    234     warnings.warn(notice, category=FutureWarning, stacklevel=2)
--> 235 return func(*args, **kwargs)

File /anaconda/envs/azureml_py310_sdkv2/lib/python3.10/site-packages/mlflow/transformers.py:822, in load_model(model_uri, dst_path, return_type, device, **kwargs)
    813     raise MlflowException(
    814         "This model has been saved with a processor. Processor objects are "
    815         "not compatible with Pipelines. Please load this model by specifying "
    816         "the 'return_type'='components'.",
    817         error_code=BAD_REQUEST,
    818     )
    820 _add_code_from_conf_to_system_path(local_model_path, flavor_config)
--> 822 return _load_model(local_model_path, flavor_config, return_type, device, **kwargs)

File /anaconda/envs/azureml_py310_sdkv2/lib/python3.10/site-packages/mlflow/transformers.py:870, in _load_model(path, flavor_config, return_type, device, **kwargs)
    865 """
    866 Loads components from a locally serialized ``Pipeline`` object.
    867 """
    868 import transformers
--> 870 model_instance = getattr(transformers, flavor_config[_PIPELINE_MODEL_TYPE_KEY])
    871 local_path = pathlib.Path(path)
    872 model_path = local_path.joinpath(flavor_config.get(_MODEL_BINARY_KEY, _MODEL_BINARY_FILE_NAME))

File /anaconda/envs/azureml_py310_sdkv2/lib/python3.10/site-packages/transformers/utils/import_utils.py:1165, in _LazyModule.__getattr__(self, name)
   1163     value = getattr(module, name)
   1164 else:
-> 1165     raise AttributeError(f"module {self.__name__} has no attribute {name}")
   1167 setattr(self, name, value)
   1168 return value

AttributeError: module transformers has no attribute BTLMLMHeadModel


Hi @amarahiqbal 
the call 

```python
model_instance = getattr(transformers, flavor_config[_PIPELINE_MODEL_TYPE_KEY])
```
won't work as you are trying to import some modules that are not registered in the transformers library that they live on the Hub.

I believe you can simply use `AutoModelForCausalLM` for your usecase and make sure to insert `trust_remote_code=True` when calling `from_pretrained`

@amarahiqbal did you get the resolution? I am also facing the same issue.?


```python
model = AutoModelForCausalLM.from_pretrained("mosaicml/mpt-7b", torch_dtype=torch.bfloat16, trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained("mosaicml/mpt-7b")

import mlflow
from mlflow.models.signature import ModelSignature
from mlflow.types.schema import ColSpec, Schema
import numpy as np

# Define the model input and output schema
input_schema = Schema([
    ColSpec("string", "prompt"),
    ColSpec("double", "temperature", optional=True),
    ColSpec("integer", "max_tokens", optional=True),
    ColSpec("string", "stop", optional=True),
    ColSpec("integer", "candidate_count", optional=True)
])

output_schema = Schema([
    ColSpec('string', 'predictions')
])

signature = ModelSignature(inputs=input_schema, outputs=output_schema)

# Define an example input
input_example = {
    "prompt": np.array([
        "Below is an instruction that describes a task. "
        "Write a response that appropriately completes the request.\n\n"
        "### Instruction:\n"
        "What is Apache Spark?\n\n"
        "### Response:\n"
    ])
}

registered_model_name = f"mpt-7b"

# Start a new MLflow run
with mlflow.start_run():
    components = {
        "model": model,
        "tokenizer": tokenizer,
    }
    mlflow.transformers.log_model(
        transformers_model=components,
        artifact_path="model",
        signature=signature,
        input_example=input_example,
        metadata={"task": "llm/v1/completions"}
    )

# After running above code. It able to log the run. But when I load it use this

logged_model = 'runs:/b7c5d5fb5a1f4c23b161578aab847cb9/model'

loaded_model = mlflow.transformers.load_model(logged_model)
loaded_model = mlflow.pyfunc.load_model(logged_model)

 # in both cases it thrown an error
```
Error:
```python
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
File <command-4055176443088148>, line 5
      2 logged_model = 'runs:/b7c5d5fb5a1f4c23b161578aab847cb9/model'
      4 # Load model as a PyFuncModel.
----> 5 loaded_model = mlflow.pyfunc.load_model(logged_model)
      7 # Predict on a Pandas DataFrame.
      8 # import pandas as pd
      9 # loaded_model.predict(pd.DataFrame(data))

File /local_disk0/.ephemeral_nfs/envs/pythonEnv-7db9db37-9b10-4f51-b86e-a7a7f12a7c41/lib/python3.10/site-packages/mlflow/pyfunc/__init__.py:637, in load_model(model_uri, suppress_warnings, dst_path)
    635 data_path = os.path.join(local_path, conf[DATA]) if (DATA in conf) else local_path
    636 try:
--> 637     model_impl = importlib.import_module(conf[MAIN])._load_pyfunc(data_path)
    638 except ModuleNotFoundError as e:
    639     if conf[MAIN] == _DATABRICKS_FS_LOADER_MODULE:

File /local_disk0/.ephemeral_nfs/envs/pythonEnv-7db9db37-9b10-4f51-b86e-a7a7f12a7c41/lib/python3.10/site-packages/mlflow/transformers.py:1544, in _load_pyfunc(path)
   1541 flavor_configuration = _get_flavor_configuration(local_path, FLAVOR_NAME)
   1542 inference_config = _get_inference_config(local_path.joinpath(_COMPONENTS_BINARY_KEY))
   1543 return _TransformersWrapper(
-> 1544     _load_model(str(local_path), flavor_configuration, "pipeline"),
   1545     flavor_configuration,
   1546     inference_config,
   1547 )

File /local_disk0/.ephemeral_nfs/envs/pythonEnv-7db9db37-9b10-4f51-b86e-a7a7f12a7c41/lib/python3.10/site-packages/mlflow/transformers.py:881, in _load_model(path, flavor_config, return_type, device, **kwargs)
    876 """
    877 Loads components from a locally serialized ``Pipeline`` object.
    878 """
    879 import transformers
--> 881 model_instance = getattr(transformers, flavor_config[_PIPELINE_MODEL_TYPE_KEY])
    882 local_path = pathlib.Path(path)
    883 # NB: Path resolution for models that were saved prior to 2.4.1 release when the pathing for
    884 #     the saved pipeline or component artifacts was handled by duplicate entries for components
    885 #     (artifacts/pipeline/* and artifacts/components/*) and pipelines were saved via the
    886 #     "artifacts/pipeline/*" path. In order to load the older formats after the change, the
    887 #     presence of the new path key is checked.

File /local_disk0/.ephemeral_nfs/envs/pythonEnv-7db9db37-9b10-4f51-b86e-a7a7f12a7c41/lib/python3.10/site-packages/transformers/utils/import_utils.py:1177, in _LazyModule.__getattr__(self, name)
   1175     value = getattr(module, name)
   1176 else:
-> 1177     raise AttributeError(f"module {self.__name__} has no attribute {name}")
   1179 setattr(self, name, value)
   1180 return value

AttributeError: module transformers has no attribute MPTForCausalLM

```


This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.