habana_frameworks.mediapipe.fn.BitwiseOr

Class:
  • habana_frameworks.mediapipe.fn.BitwiseOr(**kwargs)

Define graph call:
  • __call__(input1, input2)

Parameter:
  • input1 - First input tensor to operator. Supported dimensions: minimum = 1, maximum = 5. Supported data types: INT8, INT16, INT32, UINT8, UINT16, UINT32.

  • input2 - Second input tensor to operator. Supported dimensions: minimum = 1, maximum = 5. Supported data types: INT8, INT16, INT32, UINT8, UINT16, UINT32.

Description:

Computes the output by doing bitwise OR operation on input tensors. Output is calculated as (B = |(A)).

Supported backend:
  • HPU

Keyword Arguments

kwargs

Description

dtype

Output data type.

  • Type: habana_frameworks.mediapipe.media_types.dtype

  • Default: UINT8

  • Optional: yes

  • Supported data types:

    • UINT8

    • UINT32

    • INT8

    • INT32

Note

  1. All input/output tensors must be of the same data type and must have the same dimensionality.

  2. This operator is agnostic to the data layout.

Example: BitwiseOr Operator

The following code snippet shows usage of BitwiseOr operator:

from habana_frameworks.mediapipe import fn
from habana_frameworks.mediapipe.mediapipe import MediaPipe
from habana_frameworks.mediapipe.media_types import dtype as dt
import os
import numpy as np

# Create MediaPipe derived class


class myMediaPipe(MediaPipe):
    def __init__(self, device, queue_depth, batch_size, num_threads, op_device, dir):
        super(
            myMediaPipe,
            self).__init__(
            device,
            queue_depth,
            batch_size,
            num_threads,
            self.__class__.__name__)

        self.inp1 = fn.ReadNumpyDatasetFromDir(num_outputs=1,
                                              shuffle=False,
                                              dir=dir,
                                              pattern="inp_x_*.npy",
                                              dense=True,
                                              dtype=dt.UINT8,
                                              device="cpu")

        self.inp2 = fn.ReadNumpyDatasetFromDir(num_outputs=1,
                                              shuffle=False,
                                              dir=dir,
                                              pattern="inp_y_*.npy",
                                              dense=True,
                                              dtype=dt.UINT8,
                                              device="cpu")

        self.bitwise_or = fn.BitwiseOr(dtype=dt.UINT8,
                                      device=op_device)

    def definegraph(self):
        inp1 = self.inp1()
        inp2 = self.inp2()
        out = self.bitwise_or(inp1, inp2)
        return out, inp1, inp2


def run(device, op_device):
    batch_size = 2
    queue_depth = 2
    num_threads = 1
    base_dir = os.environ['DATASET_DIR']
    dir = base_dir+"/npy_data/u8/"

    # Create MediaPipe object
    pipe = myMediaPipe(device, queue_depth, batch_size,
                      num_threads, op_device, dir)
    # Build MediaPipe
    pipe.build()

    # Initialize MediaPipe iterator
    pipe.iter_init()

    # Run MediaPipe
    out, inp1, inp2 = pipe.run()

    def as_cpu(tensor):
        if (callable(getattr(tensor, "as_cpu", None))):
            tensor = tensor.as_cpu()
        return tensor

    def as_cpu(tensor):
        if (callable(getattr(tensor, "as_cpu", None))):
            tensor = tensor.as_cpu()
        return tensor

    # Copy data to host from device as numpy array
    out = as_cpu(out).as_nparray()
    inp1 = as_cpu(inp1).as_nparray()
    inp2 = as_cpu(inp2).as_nparray()

    del pipe

    print("\ninp1 tensor shape:", inp1.shape)
    print("inp1 tensor dtype:", inp1.dtype)
    print("inp1 tensor data:\n", inp1)

    print("\ninp2 tensor shape:", inp2.shape)
    print("inp2 tensor dtype:", inp2.dtype)
    print("inp2 tensor data:\n", inp2)

    print("\nout tensor shape:", out.shape)
    print("out tensor dtype:", out.dtype)
    print("out tensor data:\n", out)

    return inp1, inp2, out


def compare_ref(inp1, inp2, out):
    ref = np.bitwise_or(inp1, inp2)
    if np.array_equal(ref, out) == False:
        raise ValueError(f"Mismatch w.r.t ref for device")


if __name__ == "__main__":
    dev_opdev = {'mixed': ['hpu'],
                'legacy': ['hpu']}
    for dev in dev_opdev.keys():
        for op_dev in dev_opdev[dev]:
            inp1, inp2, out = run(dev, op_dev)
            compare_ref(inp1, inp2, out)

The following is the output of BitwiseOr operator:

inp1 tensor shape: (2, 3, 2, 3)
inp1 tensor dtype: uint8
inp1 tensor data:
[[[[149 187 232]
  [160 201 202]]

  [[ 80 147 153]
  [199 174 158]]

  [[200 124 139]
  [  3 161 216]]]


[[[106  93  83]
  [ 57 253  52]]

  [[222 189  26]
  [174  60 118]]

  [[218  84  43]
  [251  75  73]]]]

inp2 tensor shape: (2, 3, 2, 3)
inp2 tensor dtype: uint8
inp2 tensor data:
[[[[ 73 245  55]
  [ 65 148 105]]

  [[226 132 241]
  [ 39  76 102]]

  [[170   6 130]
  [179 101 220]]]


[[[205  68 134]
  [ 76  48  79]]

  [[118  81  33]
  [ 29 214  85]]

  [[ 50 146  23]
  [107 146  51]]]]

out tensor shape: (2, 3, 2, 3)
out tensor dtype: uint8
out tensor data:
[[[[221 255 255]
  [225 221 235]]

  [[242 151 249]
  [231 238 254]]

  [[234 126 139]
  [179 229 220]]]


[[[239  93 215]
  [125 253 127]]

  [[254 253  59]
  [191 254 119]]

  [[250 214  63]
  [251 219 123]]]]