0

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)
The Wanderer
  • 3,051
  • 6
  • 29
  • 53

1 Answers1

2

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.

Bolong
  • 164
  • 2
  • Yes, while the Python language does not have the notion of pointers outside of its C API, the arguments passed to functions are still done via pointers to `PyObject`s uder the hood. Python *does* use pointers, but they're hidden from view. Python uses pointers extensively, but they're hidden from normal use. – Carcigenicate Jun 17 '23 at 03:30