ScalarType notations in PyTorch

Could someone please help me in understanding different notations of scalar types?

For example, I see at::ScalarType::Half, kHalf, half for fp16 type in the code. Are they all identical? Couldn’t find definitions of all notations.

Yeah, that’s a bit of a mess. :slight_smile:

So these stem from c10 and the c10 namespace, but are also available in the at (and torch) namespace.

kHalf is an alias for ScalarType::Half; historically, it was common to use the kFloat, kLong,… types and there have been some refactorings until we arrived at the ScalarType we have today (or at least t.scalar_type() is newer than some PyTorch code). ScalarType::Half it is a type identifier (ScalarType is the C++ side fo dtype).

Half is a type like int64_t, it matches half or __half when the platform has one and provides the usual ops. This is an abstraction that allows templating to include Half where one would have to do crazy dances around not defined things otherwise).

Best regards

Thomas

1 Like

Thanks Thomas for the reply. Could you please point me to the code where kHalf alias is defined?

It is right there in c10/core/ScalarType.h, but it is not easy to find because it uses some macro magic.

Best regards

Thomas

1 Like