habana_frameworks.mediapipe.fn.ReduceMax
habana_frameworks.mediapipe.fn.ReduceMax¶
- Class:
habana_frameworks.mediapipe.fn.ReduceMax(**kwargs)
- Define graph call:
__call__(input)
- Parameter:
input - Input tensor to operator. Supported dimensions: minimum = 1, maximum = 5. Supported data types: INT32, FLOAT16, BFLOAT16, FLOAT32.
Description:
Computes the maximum of the input tensor’s elements along the provided dimension. Along with reduced output tensor, the index tensor (Retained-Index) is also produced. The resultant tensor stores the index of minimum element.
- Supported backend:
HPU
Keyword Arguments
kwargs |
Description |
---|---|
reductionDimension |
The dimension in which to perform the reduction operation.
|
dtype |
Output data type.
|
Note
All input/output tensors must be of the same data type.
All tensors should have the same dimensionality.
Size of the reduction dimension in the output tensor must be 1.
The size of the index tensor must be equal to the size of the output tensor.
Example: ReduceMax Operator
The following code snippet shows usage of ReduceMax 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 numpy as np
import os
# Create MediaPipe derived class
class myMediaPipe(MediaPipe):
def __init__(self, device, queue_depth, batch_size, num_threads, op_device, dir, pattern):
super(
myMediaPipe,
self).__init__(
device,
queue_depth,
batch_size,
num_threads,
self.__class__.__name__)
self.inputxy = fn.ReadNumpyDatasetFromDir(num_outputs=1,
shuffle=False,
dir=dir,
pattern=pattern,
dtype=dt.FLOAT32,
device="cpu")
self.rmax = fn.ReduceMax(reductionDimension=0,
dtype=dt.FLOAT32,
device=op_device)
def definegraph(self):
img = self.inputxy()
rmax, rmax_idx = self.rmax(img)
return rmax, rmax_idx, img
def run(device, op_device):
batch_size = 2
queue_depth = 1
num_threads = 1
base_dir = os.environ['DATASET_DIR']
dir = base_dir+"/npy_data/fp32/"
pattern = "*x*.npy"
# Create MediaPipe object
pipe = myMediaPipe(device, queue_depth, batch_size, num_threads,
op_device, dir, pattern)
# Build MediaPipe
pipe.build()
# Initialize MediaPipe iterator
pipe.iter_init()
# Run MediaPipe
rmax, rmax_idx, img = pipe.run()
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
img = as_cpu(img).as_nparray()
rmax = as_cpu(rmax).as_nparray()
rmax_idx = as_cpu(rmax_idx).as_nparray()
del pipe
# Display shape, data
print('input shape:', img.shape)
print('input data:', img)
print('output shape:', rmax.shape)
print('output data:\n', rmax)
return img, rmax, rmax_idx
def compare_ref(inp, rmax, rmax_idx):
ref = np.amax(inp, axis=-1, keepdims=True)
ref_idx = np.argmax(inp, axis=-1, keepdims=True)
if np.array_equal(ref, rmax) == False:
raise ValueError(f"Val Mismatch w.r.t ref for device")
if np.array_equal(ref_idx, rmax_idx) == False:
raise ValueError(f"Val 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]:
inp, rmax, rmax_idx = run(dev, op_dev)
compare_ref(inp, rmax, rmax_idx)
The following is the output for ReduceMax operator:
input shape: (2, 3, 2, 3)
input data: [[[[182. 227. 113.]
[175. 128. 253.]]
[[ 58. 140. 136.]
[ 86. 80. 111.]]
[[175. 196. 178.]
[ 20. 163. 108.]]]
[[[186. 254. 96.]
[180. 64. 132.]]
[[149. 50. 117.]
[213. 6. 111.]]
[[ 77. 11. 160.]
[129. 102. 154.]]]]
output shape: (2, 3, 2, 1)
output data:
[[[[227.]
[253.]]
[[140.]
[111.]]
[[196.]
[163.]]]
[[[254.]
[180.]]
[[149.]
[213.]]
[[160.]
[154.]]]]