Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clip convert error: Dynamic value of min/max is not implemented #191

Open
ongiaf opened this issue Dec 15, 2023 · 8 comments
Open

Clip convert error: Dynamic value of min/max is not implemented #191

ongiaf opened this issue Dec 15, 2023 · 8 comments

Comments

@ongiaf
Copy link

ongiaf commented Dec 15, 2023

Hi,
I have an onnx model. Here is one of the nodes in onnxgraph

node {
  input: "/decoder.0/layers.0/blocks.0/attn/Pow_3_output_0"
  input: "/decoder.0/layers.0/blocks.0/attn/Constant_13_output_0"
  input: ""
  output: "/decoder.0/layers.0/blocks.0/attn/Clip_1_output_0"
  name: "/decoder.0/layers.0/blocks.0/attn/Clip_1"
  op_type: "Clip"
  doc_string: "...."
}

When I tried to convert it to the torch model, it cased a KeyError:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/root/micromamba/envs/ai-models/lib/python3.10/site-packages/onnx2torch/converter.py", line 110, in convert
    torch_module, onnx_mapping = converter(onnx_node, onnx_graph)
  File "/root/micromamba/envs/ai-models/lib/python3.10/site-packages/onnx2torch/node_converters/clip.py", line 60, in _
    raise NotImplementedError('Dynamic value of min/max is not implemented') from exc
NotImplementedError: Dynamic value of min/max is not implemented

it may caused by

raise NotImplementedError('Dynamic value of min/max is not implemented') from exc

After adding conditions

min_val = float(get_const_value(min_name, graph)) if (min_name is not None and min_name != '') else None
max_val = float(get_const_value(max_name, graph)) if (max_name is not None and max_name != '') else None

The convert can work.

@ongiaf
Copy link
Author

ongiaf commented Dec 15, 2023

@dsuhoi
Copy link

dsuhoi commented Feb 6, 2024

@ongiaf Do you have any success decoding FuXi (I've been fine-tuning this model for a long time)? I recommend paying attention to this solution

@ongiaf
Copy link
Author

ongiaf commented Feb 7, 2024

Thanks, it's excellent work.
And with some dirty work, Fuxi can successfully run on PyTorch with Onnx2Torch. In Onnx2Torch, problems are mainly about LayerNormalization and Clip.

@dsuhoi
Copy link

dsuhoi commented Feb 7, 2024

@ongiaf Did you manage to run FuXi with the current weights for the fine-tuning process (I am currently thinking about how to complete the work on the model on a 1-hour grid and thought about freezing some layers except the U-transformer.) ?

@juanqiu1
Copy link

Thanks, it's excellent work. And with some dirty work, Fuxi can successfully run on PyTorch with Onnx2Torch. In Onnx2Torch, problems are mainly about LayerNormalization and Clip.

Thank you for posting your changes about Clip. Could you also suggest how to fix LayerNormalization? Looks like the converted model has issue with torch.layer_norm call.

@dsuhoi
Copy link

dsuhoi commented Feb 20, 2024

@juanqiu1 In order for this to work with FuXi, you will need to change the onnx2torch/node_converters/layer_norm.py parameter to [1536]:

@add_converter(operation_type='LayerNormalization', version=17)
def _(node: OnnxNode, graph: OnnxGraph) -> OperationConverterResult:
    node_attributes = node.attributes

    axis = node_attributes.get('axis', AXIS_DEFAULT_VALUE)
    epsilon = node_attributes.get('epsilon', EPSILON_DEFAULT_VALUE)

    if all(value_name in graph.initializers for value_name in node.input_values[1:]):
        input_value_info = graph.value_info[node.input_values[0]]
        input_shape = get_shape_from_value_info(input_value_info)
        torch_module = nn.LayerNorm(
            normalized_shape=(1536), # input_shape[axis:], (this block!)
            eps=epsilon,
            elementwise_affine=True,
        )

@juanqiu1
Copy link

@dsuhoi Thank you for hinting, there are a couple of other easy fixes (typing, etc).
Did you manage to run FuXi with the current weights for the fine-tuning process
Do you have any progress on that? After conversion, I loaded model into pytorch but even on A100 with FSDP enabled via accelerate. I still get CUDA out-of-memory error.

@dsuhoi
Copy link

dsuhoi commented Feb 21, 2024

@juanqiu1 Yes, I managed to start the learning process by highlighting the named_parameters() part within the last dozen UTransformer (this was enough for fine-tunning).

I used Nvidia A100 (40GB).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants