def is_transposed_and_fusable_to_matmul(x: Var) -> bool:
        """
        1. check if x is transposed
        2. check if x is transposed in the last 2 dimensions,
           since the transpose arg in matmul only transposes the last 2 dimensions
        """

        # x is not transposed, False
        if x.op is None or x.op.op_type != "transpose":
            return False

        rank = x.rank
        # if transposing a rank < 2 tensor, it is a noop and will be elimianted by noop_elimination
        if rank < 2:
            return False

        # canonicalize the input permutation to compare with last-2-dim permutation below
        perm = x.op.perm.val
        perm[np.where(perm < 0)] += rank
        perm[-2:] -= rank

        # permuting only last 2 dims should look like (0, 1, ..., -1, -2)
        perm_only_last_2_dims = np.arange(rank)
        perm_only_last_2_dims[-2] = -1
        perm_only_last_2_dims[-1] = -2

        return np.all(perm == perm_only_last_2_dims)

        1. check if x is transposed
        2. check if x is transposed in the last 2 dimensions,
           since the transpose arg in matmul only transposes the last 2 dimensions
        
coremltools/converters/mil/mil/passes/defs/optimize_linear.py