# TypeError: model\_fn() takes 1 positional argument but 2 were given

**URL:** <https://discuss.huggingface.co/t/typeerror-model-fn-takes-1-positional-argument-but-2-were-given/76391>\
**Category:** Amazon SageMaker\
**Created:** [March 7, 2024, 1:11pm UTC](https://discuss.huggingface.co/t/typeerror-model-fn-takes-1-positional-argument-but-2-were-given/76391 "2024-03-07T13:11:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![akshat-kumar-akight](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@akshat-kumar-akight](https://discuss.huggingface.co/u/akshat-kumar-akight)\
**Post date:** [March 7, 2024, 1:11pm UTC](https://discuss.huggingface.co/t/typeerror-model-fn-takes-1-positional-argument-but-2-were-given/76391/1 "2024-03-07T13:11:44Z")

</div>

I am trying to deploy Sagemaker Endpoint with custom scripts using model\_fn, predict fn and input\_fn using sqlcoder7b-2 and I am getting error  
`TypeError: model_fn() takes 1 positional argument but 2 were given`

```auto
#code/inference.py
def model_fn(model_dir):
    tokenizer = AutoTokenizer.from_pretrained(model_dir)
    model = AutoModelForCausalLM.from_pretrained(
        model_dir,
        trust_remote_code=True,
        torch_dtype=torch.float16,
        device_map="auto",
        use_cache=True,
    cache_dir ="sqlcoder",
    offload_folder="offload_sqlcoder",
    force_download=True
    )
    model_dict = {'model':model, 'tokenizer':tokenizer}
    return model_dict 

def predict_fn(data, model_dict):
    tokenizer = model_dict['tokenizer']
    model = model_dict['model']
    question=data["inputs"]
    print("Question is",question)
    print("Prompt is",prompt)
    updated_prompt = prompt.format(question=question)
    print("Updated Prompt is",prompt)
    inputs = tokenizer(updated_prompt, return_tensors="pt").to("cuda")
    generated_ids = model.generate(
        **inputs,
        num_return_sequences=1,
        eos_token_id=tokenizer.eos_token_id,
        pad_token_id=tokenizer.eos_token_id,
        max_new_tokens=400,
        do_sample=False,
        num_beams=1,
    )
    outputs = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)

    torch.cuda.empty_cache()
    torch.cuda.synchronize()
    return sqlparse.format(outputs[0].split("[SQL]")[-1], reindent=True)

    
def input_fn(request_body, request_content_type):
    # Transform the input request to a dictionary
    print("Request body is",request_body)
    print("Request Content Type is", request_content_type)
    request = json.loads(request_body)
    
    return request

```

```auto
#Deployment Script
from sagemaker.huggingface.model import HuggingFaceModel

hub = {
# 'HF_MODEL_ID':'defog/sqlcoder-7b-2', # model_id from hf.co/models
  'HF_TASK':'text-generation' # NLP task you want to use for predictions
}
# create Hugging Face Model Class
huggingface_model = HuggingFaceModel(
   model_data=s3_location, # path to your model and script
   role=role, # iam role with permissions to create an Endpoint
   transformers_version="4.37.0", # transformers version used
   pytorch_version="2.1.0", # pytorch version used
   py_version='py310', # python version used
   env=hub
)

# deploy the endpoint endpoint
predictor = huggingface_model.deploy(
    initial_instance_count=1,
    instance_type="ml.g5.xlarge"
    )

```

---

<div class="post-metadata">

**Author:** ![bee133](https://avatars.discourse-cdn.com/v4/letter/b/50afbb/32.png) [@bee133](https://discuss.huggingface.co/u/bee133)\
**Post date:** [March 13, 2024, 12:26am UTC](https://discuss.huggingface.co/t/typeerror-model-fn-takes-1-positional-argument-but-2-were-given/76391/2 "2024-03-13T00:26:44Z")

</div>

Did you fix the issue?

---

<div class="post-metadata">

**Author:** ![akshat-kumar-akight](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@akshat-kumar-akight](https://discuss.huggingface.co/u/akshat-kumar-akight)\
**Post date:** [March 13, 2024, 6:39pm UTC](https://discuss.huggingface.co/t/typeerror-model-fn-takes-1-positional-argument-but-2-were-given/76391/3 "2024-03-13T18:39:23Z")

</div>

Yes, I was able to resolve the issue. I removed my function “input\_fn” from the script (this meant that I ended up using “input” func written in sagemaker huggingface inference toolkit). In “model\_fn” and “predict\_fn”, I added an argument “context=None”. In predict\_fn, I modified argument “model\_dict” to “model”.

---

<div class="post-metadata">

**Author:** ![jacobwindle](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@jacobwindle](https://discuss.huggingface.co/u/jacobwindle)\
**Post date:** [April 2, 2024, 1:53pm UTC](https://discuss.huggingface.co/t/typeerror-model-fn-takes-1-positional-argument-but-2-were-given/76391/4 "2024-04-02T13:53:48Z")

</div>

How did you know to do this? I’m currently facing this issue having just redeployed a previously working endpoint + endpoint configuration. Not sure what the issue is.

---

<div class="post-metadata">

**Author:** ![akshat-kumar-akight](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@akshat-kumar-akight](https://discuss.huggingface.co/u/akshat-kumar-akight)\
**Post date:** [April 2, 2024, 2:19pm UTC](https://discuss.huggingface.co/t/typeerror-model-fn-takes-1-positional-argument-but-2-were-given/76391/5 "2024-04-02T14:19:28Z")

</div>

This code is executed for inference. You can see lines 277 to 281 where they have defined functions load\_fn, predict\_fn etc. These functions are like user defined proxies to load, predict and other huggingface functions. Just one constraint that these user proxy functions should take the arguments passed by respective huggingface defined functions.

> <https://github.com/aws/sagemaker-huggingface-inference-toolkit/blob/main/src/sagemaker_huggingface_inference_toolkit/handler_service.py>

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/hellohellohello/original/2X/d/de4155eb4aa4108ecb32a1389d7cc37ae69f88b7.png) [@system](https://discuss.huggingface.co/u/system)\
**Post date:** [April 4, 2024, 2:30pm UTC](https://discuss.huggingface.co/t/typeerror-model-fn-takes-1-positional-argument-but-2-were-given/76391/6 "2024-04-04T14:30:40Z")

</div>

Closed on April 4, 2024, 2:30pm UTC

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
