Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[JAX FE] Report all unsupported operations during conversion (#26234)

Roman Kazantseve38995a70c96
diff --git a/src/bindings/python/src/openvino/frontend/jax/utils.py b/src/bindings/python/src/openvino/frontend/jax/utils.py
index 48c69f4265a..0ba59634b9f 100644
--- a/src/bindings/python/src/openvino/frontend/jax/utils.py
+++ b/src/bindings/python/src/openvino/frontend/jax/utils.py
@@ -5,16 +5,15 @@
# mypy: ignore-errors
import jax
-import numpy as np
import jax.numpy as jnp
-
-from openvino.runtime import op, Type as OVType, Shape, OVAny
+import numpy as np
from openvino.frontend.jax.passes import filter_element, filter_ivalue, filter_param
+from openvino.runtime import op, Type as OVType, Shape, OVAny
numpy_to_ov_type_map = {
np.float32: OVType.f32,
bool: OVType.boolean,
- jax.dtypes.bfloat16: OVType.bf16, # TODO: check this
+ jax.dtypes.bfloat16: OVType.bf16, # TODO: check this
np.float16: OVType.f16,
np.float32: OVType.f32,
np.float64: OVType.f64,
@@ -27,7 +26,7 @@ numpy_to_ov_type_map = {
jax_to_ov_type_map = {
jnp.float32: OVType.f32,
- jnp.bfloat16: OVType.bf16, # TODO: check this
+ jnp.bfloat16: OVType.bf16, # TODO: check this
jnp.float16: OVType.f16,
jnp.float64: OVType.f64,
jnp.uint8: OVType.u8,
@@ -61,6 +60,7 @@ ov_type_to_int_map = {
OVType.bf16: 15,
}
+
def get_type_from_py_type(value):
if isinstance(value, float):
return OVType.f32
@@ -70,18 +70,21 @@ def get_type_from_py_type(value):
return OVType.i64
return OVType.dynamic
+
def get_type_from_np_type(value):
for np_dtype, ov_type in numpy_to_ov_type_map.items():
if isinstance(value, np_dtype):
return ov_type
return None
+
def _get_ov_type_from_value(value):
ov_type = get_type_from_np_type(value)
if ov_type is None:
ov_type = get_type_from_py_type(value)
return ov_type
+
def get_ov_type_for_value(value):
if isinstance(value, (jax.core.Var, jax.core.Literal)):
if value.aval.dtype in jax_to_ov_type_map:
@@ -96,7 +99,8 @@ def get_ov_type_for_value(value):
return OVAny(jax_to_ov_type_map[type(value)])
else:
raise NotImplementedError(f"dtype for {value} of type {type(value)} has not been supported yet.")
-
+
+
def get_ov_type_from_jax_type(dtype):
if dtype in jax_to_ov_type_map:
return OVAny(jax_to_ov_type_map[dtype])
@@ -108,6 +112,7 @@ def get_ov_type_from_jax_type(dtype):
return OVAny(v)
return None
+
def jax_array_to_ov_const(arr: np.ndarray, shared_memory=True):
# TODO: deal with bfloat16 dtype here.
if isinstance(arr, np.ndarray):
@@ -117,10 +122,12 @@ def jax_array_to_ov_const(arr: np.ndarray, shared_memory=True):
else:
raise ValueError(f"Constant is expected to be a numpy array or jax array but got {type(arr)}")
+
def ivalue_to_constant(ivalue, shared_memory=True):
'''
Convert a python object to an openvino constant.
'''
+ # print('ivalue = ', ivalue)
ivalue = filter_ivalue(ivalue)
ov_type = _get_ov_type_from_value(ivalue)
if ov_type.is_static():
@@ -140,21 +147,27 @@ def ivalue_to_constant(ivalue, shared_memory=True):
return op.Constant(ov_type, Shape([len(ivalue), second_len]), flattened_ivalue).outputs()
ivalue = [filter_element(item) for item in ivalue]
ov_type = _get_ov_type_from_value(ivalue[0])
- assert ov_type.is_static(), f"Can't deduce type {ivalue[0].__class__} for list"
+ try:
+ assert ov_type.is_static(), f"Can't deduce type {ivalue[0].__class__} for list"
+ except:
+ # TODO 150596: remove this workaround
+ ivalue = [0]
+ ov_type = OVType.f32
return op.Constant(ov_type, Shape([len(ivalue)]), ivalue).outputs()
if isinstance(ivalue, (jax.Array, np.ndarray)):
return jax_array_to_ov_const(ivalue, shared_memory=shared_memory).outputs()
-
+
ov_dtype_value = get_ov_type_from_jax_type(ivalue)
if ov_dtype_value is not None:
return op.Constant(OVType.i64, Shape([]), [ov_type_to_int_map[ov_dtype_value]]).outputs()
-
+
return None
+
def param_to_constants(primitive: str, param_name: str, jaxpr, shared_memory=True):
processed_params = filter_param(primitive, param_name, jaxpr)
-
+
for k, v in processed_params.items():
processed_params[k] = ivalue_to_constant(v, shared_memory=shared_memory)
- return processed_params
\ No newline at end of file
+ return processed_params
diff --git a/src/frontends/jax/src/translate_session.cpp b/src/frontends/jax/src/translate_session.cpp
index a83dd87c3e1..5545f30921f 100644
--- a/src/frontends/jax/src/translate_session.cpp
+++ b/src/frontends/jax/src/translate_session.cpp
@@ -191,17 +191,12 @@ OutputVector TranslateSession::convert_node(const NodeContext& context) {
OPENVINO_DEBUG("No translator found for: ", context.get_op_type(), "\n");
} catch (std::exception& e) {
exception = e.what();
- if (m_telemetry) {
- auto cropped_message = ov::util::filter_lines_by_prefix(exception, get_jax_prefix());
- if (cropped_message.size()) {
- m_telemetry->send_event("error_info", cropped_message);
- }
- }
} catch (...) {
exception = "Unknown exception type.";
}
- exception += "Failed to convert operation: " + context.get_op_type() + ". Reason: ";
- FRONT_END_THROW(exception);
+ OPENVINO_DEBUG(exception, "\n");
+ // Create JaxFrameworkNode for everything that wasn't able to be converted normally
+ return make_framework_node(context, exception);
}
void TranslateSession::encode_tensor_name(Output<Node> output,
diff --git a/src/frontends/jax/src/utils.cpp b/src/frontends/jax/src/utils.cpp
index 08959e79bf0..d1092a37dcf 100644
--- a/src/frontends/jax/src/utils.cpp
+++ b/src/frontends/jax/src/utils.cpp
@@ -54,6 +54,23 @@ const std::unordered_map<int64_t, element::Type> JAX_TO_OV_TYPE{
{14, element::i32}, // quantized i32
{15, element::bf16},
};
+
+std::shared_ptr<JaxFrameworkNode> create_fw_node_with_exception(const NodeContext& context,
+ const ov::OutputVector& inputs,
+ size_t num_outputs,
+ const std::string& exception_message,
+ bool skip_subgraphs = false) {
+ auto fw_node = std::make_shared<JaxFrameworkNode>(context.get_decoder(), inputs, num_outputs);
+ auto attrs = fw_node->get_attrs();
+ std::string message(exception_message);
+ if (!message.empty()) {
+ message = "Exception happened during conversion of operation " + fw_node->get_friendly_name() + '\n' + message;
+ }
+ attrs[JaxFrameworkNode::failed_conversion_key] = message;
+ fw_node->set_attrs(attrs);
+ return fw_node;
+}
+
} // namespace
element::Type convert_dtype(int64_t jax_type) {
@@ -61,6 +78,13 @@ element::Type convert_dtype(int64_t jax_type) {
return JAX_TO_OV_TYPE.at(jax_type);
};
+OutputVector make_framework_node(const NodeContext& context, const std::string& exception) {
+ // We create additional output for such nodes. It contains new tensor that represents input that was changed.
+ auto fw_node = create_fw_node_with_exception(context, context.inputs(), context.get_output_size() + 1, exception);
+ auto outputs = fw_node->outputs();
+ return outputs;
+}
+
} // namespace jax
} // namespace frontend
} // namespace ov
diff --git a/src/frontends/jax/src/utils.hpp b/src/frontends/jax/src/utils.hpp
index 9889a87a046..518489ed681 100644
--- a/src/frontends/jax/src/utils.hpp
+++ b/src/frontends/jax/src/utils.hpp
@@ -43,6 +43,8 @@ bool is_python_scalar_input(const NodeContext& context, size_t index);
element::Type convert_dtype(int64_t pt_type);
+OutputVector make_framework_node(const NodeContext& context, const std::string& exception);
+
namespace op {
template <typename T>