Skip to content

Bitwise operators and logical operators naming (rename not to logicalNot) #496

@fdwr

Description

@fdwr

(separated from my comment here https://github.com/webmachinelearning/webnn/pull/478/files#r1406997415)

Looking ahead, WebNN will eventually want to implement the bitwise operators for completeness as the more mature ML libraries have them, and as the "bitwise" ops are more fundamental than the "logical" ones (the logical ones are a limited subset that can be implemented in terms of the bitwise ones). See the table below for "bitwise" operators and "logical" counterparts. In the transformers support thread, I only proposed the minimum needed operators for the pertinent models (just logicalNot) rather than the full collection of them, but it's prudent to look holistically when naming them.

Shortening the names (e.g. trimming bitwiseAnd or logicalAnd -> just and) leads to ambiguity of which of the two "and"'s is meant. That's probably why most libraries spell them out to disambiguate. One ML library, ONNX, chose the shorter form like And to mean a LogicalAnd, but it just as easily could have arbitrarily blessed BitwiseAnd instead (and arguably that would have made sense because BitwiseAnd is the generic op). So, to reduce ambiguity and increase consistency with library precedent, I think we shouldn't shorten the name "logicalNot" to just "not".

Bitwise ops Logical ops
PyTorch
torch.bitwise_and torch.logical_and
torch.bitwise_or torch.logical_or
torch.bitwise_xor torch.logical_xor
torch.bitwise_not torch.logical_not
torch.bitwise_left_shift -
torch.bitwise_right_shift -
TensorFlow (full)
tf.bitwise.bitwise_and tf.math.logical_and
tf.bitwise.bitwise_or tf.math.logical_or
tf.bitwise.bitwise_xor tf.math.logical_xor
tf.bitwise.invert tf.math.logical_not
tf.bitwise.left_shift -
tf.bitwise.right_shift -
TensorFlow Lite
kTfLiteBuiltinStablehloAnd kTfLiteBuiltinLogicalAnd
kTfLiteBuiltinStablehloOr kTfLiteBuiltinLogicalOr
kTfLiteBuiltinBitwiseXor ❌ *no logical xor 2024-10-02, but emulatable
❌ *no bitwise not 2024-10-02, but emulatable kTfLiteBuiltinLogicalNot
kTfLiteBuiltinStablehloShiftLeft -
kTfLiteBuiltinRightShift -
NumPy
numpy.bitwise_and numpy.logical_and
numpy.bitwise_or numpy.logical_or
numpy.bitwise_xor numpy.logical_xor
numpy.invert numpy.logical_not
numpy.left_shift -
numpy.right_shift -
TOSA
tosa.BITWISE_AND tosa.LOGICAL_AND
tosa.BITWISE_OR tosa.LOGICAL_OR
tosa.BITWISE_XOR tosa.LOGICAL_XOR
tosa.BITWISE_NOT tosa.LOGICAL_NOT
tosa.LOGICAL_LEFT_SHIFT -
tosa.LOGICAL_RIGHT_SHIFT -
DirectML
DML_ELEMENT_WISE_BIT_AND DML_ELEMENT_WISE_LOGICAL_AND
DML_ELEMENT_WISE_BIT_OR DML_ELEMENT_WISE_LOGICAL_OR
DML_ELEMENT_WISE_BIT_XOR DML_ELEMENT_WISE_LOGICAL_XOR
DML_ELEMENT_WISE_BIT_NOT DML_ELEMENT_WISE_LOGICAL_NOT
DML_ELEMENT_WISE_BIT_SHIFT_LEFT -
DML_ELEMENT_WISE_BIT_SHIFT_RIGHT -
ONNX
ONNX BitwiseAnd ONNX And
ONNX BitwiseOr ONNX Or
ONNX BitwiseXor ONNX Xor
ONNX BitwiseNot ONNX Not
ONNX BitShift LEFT -
ONNX BitShift RIGHT -
CoreML
❌ no bitwise and 2024-10-02 CoreML LogicalAndLayerParams
❌ no bitwise or 2024-10-02 CoreML LogicalOrLayerParams
❌ no bitwise xor 2024-10-02 CoreML LogicalXorLayerParams
❌ no bitwise not 2024-10-02, but try negate(x)-1 CoreML LogicalNotLayerParams
❌ no left-shift 2024-10-02, but try x*2 -
❌ no right-shift 2024-10-02, but try x/2 -

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions