import numpy as np
import torch
__golden__ = {
"kernel": {"trace": "trace_golden"},
"aclnn": {"aclnnTrace": "aclnn_trace_golden"},
}
def trace_golden(x, **kwargs):
"""
Golden function for trace.
All the parameters (names and order) follow @trace_def.cpp without outputs.
All the input Tensors are numpy.ndarray.
Args:
**kwargs: {input,output}_{dtypes,ori_shapes,formats,ori_formats},
full_soc_version, short_soc_version, testcase_name
Returns:
Output tensor
"""
x_dtype = x.dtype
output_dtype = None
declared_output_dtypes = kwargs.get("output_dtypes")
if declared_output_dtypes:
declared_output_dtype = declared_output_dtypes[0]
try:
output_dtype = np.dtype(declared_output_dtype)
except (TypeError, ValueError):
output_dtype = None
if x_dtype.name in ("bfloat16", "float16"):
x_fp32 = x.astype(np.float32)
x_torch = torch.from_numpy(x_fp32)
result = torch.trace(x_torch)
target_dtype = output_dtype if output_dtype is not None else x_dtype
output = result.numpy().astype(target_dtype, copy=False)
elif x_dtype.name in ("uint16", "uint32", "bool"):
x_i64 = x.astype(np.int64)
x_torch = torch.from_numpy(x_i64)
result = torch.trace(x_torch)
output = result.numpy()
else:
x_torch = torch.from_numpy(x)
result = torch.trace(x_torch)
output = result.numpy()
if output_dtype is not None and output.dtype != output_dtype:
output = output.astype(output_dtype, copy=False)
return output
def _aclnn_trace_impl(self_tensor):
"""Common implementation for aclnnTrace.
torch.trace() on CPU does not support float16/bfloat16/uint16/uint32/bool.
Compute in a compatible dtype instead.
"""
if self_tensor.dtype in (torch.float16, torch.bfloat16):
return torch.trace(self_tensor.to(torch.float32))
elif self_tensor.dtype == torch.bool:
return torch.trace(self_tensor.to(torch.int64))
else:
return torch.trace(self_tensor)
def aclnn_trace_golden(selfT, out, **kwargs):
"""
Aclnn golden for aclnnTrace.
All the parameters (name & order) follow \
function `aclnnTraceGetWorkspaceSize` in @aclnn_trace.h \
without `workspaceSize` & `executor`.
When all dtypes are natively supported by torch, \
the Tensors in the parameters are all torch.Tensor. \
Conversely, when not, the Tensors in the parameters are all numpy.ndarray.
Args:
kwargs: tensor_{dtypes, formats}, scalar_dtypes, short_soc_version, testcase_name
Returns:
Output tensors.
"""
if isinstance(selfT, np.ndarray):
self_dtype = selfT.dtype
if self_dtype.name in ("bfloat16", "float16"):
self_fp32 = selfT.astype(np.float32)
self_torch = torch.from_numpy(self_fp32)
elif self_dtype.name in ("uint16", "uint32", "bool"):
self_i64 = selfT.astype(np.int64)
self_torch = torch.from_numpy(self_i64)
else:
self_torch = torch.from_numpy(selfT)
else:
self_torch = selfT
result = _aclnn_trace_impl(self_torch)
if isinstance(out, np.ndarray):
if isinstance(result, torch.Tensor):
result = result.detach().cpu().numpy()
return np.asarray(result).astype(out.dtype, copy=False)
if isinstance(result, np.ndarray):
result = torch.from_numpy(result)
return result.to(dtype=out.dtype)