Search code examples
pythonpointershuggingface-transformershuggingface

Passing Dicts using Pointers in Python HuggingFace


AFAIK, in Python objects are passed by reference, then why do HuggingFace keeps using pointers to pass objects? Example snippet below taken from the tutorial at this link: https://huggingface.co/learn/nlp-course/chapter3/4?fw=pt

raw_inputs = [
    "I've been waiting for a HuggingFace course my whole life.",
    "I hate this so much!",
]
inputs = tokenizer(raw_inputs, padding=True, truncation=True, return_tensors="pt")
print(inputs)

from transformers import AutoModel

checkpoint = "distilbert-base-uncased-finetuned-sst-2-english"
model = AutoModel.from_pretrained(checkpoint)

outputs = model(**batch)  #  <-- What does this even mean? 
print(outputs.loss, outputs.logits.shape)

Solution

  • func(**kwargs) is passing dictionary kwargs as keyword (non-positional) arguments to func.

    If func is defined as such:

    def func(arg1, arg2, arg3):
        pass
    

    And dict kwargs is such:

    kwargs = {
        "arg1": value1,
        "arg2": value2,
        "arg3": value3,
    }
    

    Then calling func(**kwargs) is equivalent to calling func(arg1=value1, arg2=value2, arg3=value3).

    It has nothing to do with pointers. Python does not have pointers.