|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2025 the HuggingFace Team. All rights reserved. |
| 3 | +# |
| 4 | +# This code is adapted from https://github.com/huggingface/transformers |
| 5 | +# with modifications to run transformers on mindspore. |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +"""Testing suite for the Mindspore FlexOlmo model.""" |
| 19 | + |
| 20 | +import numpy as np |
| 21 | +import pytest |
| 22 | +import torch |
| 23 | +from transformers.models.flex_olmo.configuration_flex_olmo import FlexOlmoConfig |
| 24 | + |
| 25 | +import mindspore as ms |
| 26 | + |
| 27 | +from mindone.transformers.models.flex_olmo import FlexOlmoModel |
| 28 | +from tests.modeling_test_utils import compute_diffs, generalized_parse_args, get_modules |
| 29 | + |
| 30 | +from ...causal_lm_tester import CausalLMModelTester |
| 31 | + |
| 32 | +DTYPE_AND_THRESHOLDS = {"fp32": 5e-4, "fp16": 5e-3, "bf16": 5e-2} |
| 33 | +MODES = [1] # not support graph mode yet |
| 34 | + |
| 35 | + |
| 36 | +class FlexOlmoModelTester(CausalLMModelTester): |
| 37 | + base_model_class = FlexOlmoModel |
| 38 | + config_class = FlexOlmoConfig |
| 39 | + |
| 40 | + |
| 41 | +model_tester = FlexOlmoModelTester() |
| 42 | +( |
| 43 | + config, |
| 44 | + input_ids, |
| 45 | + token_type_ids, |
| 46 | + input_mask, |
| 47 | + sequence_labels, |
| 48 | + token_labels, |
| 49 | + choice_labels, |
| 50 | +) = model_tester.prepare_config_and_inputs() |
| 51 | + |
| 52 | + |
| 53 | +FLEXOLMO_CASES = [ |
| 54 | + [ |
| 55 | + "FlexOlmoModel", |
| 56 | + "transformers.FlexOlmoModel", |
| 57 | + "mindone.transformers.FlexOlmoModel", |
| 58 | + (config,), |
| 59 | + {}, |
| 60 | + (), |
| 61 | + { |
| 62 | + "input_ids": input_ids, |
| 63 | + "attention_mask": input_mask, |
| 64 | + }, |
| 65 | + { |
| 66 | + "last_hidden_state": 0, |
| 67 | + }, |
| 68 | + ], |
| 69 | +] |
| 70 | + |
| 71 | + |
| 72 | +# transformers need >= 4.41.2 |
| 73 | +@pytest.mark.parametrize( |
| 74 | + "name,pt_module,ms_module,init_args,init_kwargs,inputs_args,inputs_kwargs,outputs_map,dtype,mode", |
| 75 | + [ |
| 76 | + case |
| 77 | + + [ |
| 78 | + dtype, |
| 79 | + ] |
| 80 | + + [ |
| 81 | + mode, |
| 82 | + ] |
| 83 | + for case in FLEXOLMO_CASES |
| 84 | + for dtype in DTYPE_AND_THRESHOLDS.keys() |
| 85 | + for mode in MODES |
| 86 | + ], |
| 87 | +) |
| 88 | +def test_named_modules( |
| 89 | + name, |
| 90 | + pt_module, |
| 91 | + ms_module, |
| 92 | + init_args, |
| 93 | + init_kwargs, |
| 94 | + inputs_args, |
| 95 | + inputs_kwargs, |
| 96 | + outputs_map, |
| 97 | + dtype, |
| 98 | + mode, |
| 99 | +): |
| 100 | + ms.set_context(mode=mode) |
| 101 | + |
| 102 | + ( |
| 103 | + pt_model, |
| 104 | + ms_model, |
| 105 | + pt_dtype, |
| 106 | + ms_dtype, |
| 107 | + ) = get_modules(pt_module, ms_module, dtype, *init_args, **init_kwargs) |
| 108 | + pt_inputs_args, pt_inputs_kwargs, ms_inputs_args, ms_inputs_kwargs = generalized_parse_args( |
| 109 | + pt_dtype, ms_dtype, *inputs_args, **inputs_kwargs |
| 110 | + ) |
| 111 | + |
| 112 | + # set `hidden_dtype` if requiring, for some modules always compute in float |
| 113 | + # precision and require specific `hidden_dtype` to cast before return |
| 114 | + with torch.no_grad(): |
| 115 | + pt_outputs = pt_model(*pt_inputs_args, **pt_inputs_kwargs) |
| 116 | + ms_outputs = ms_model(*ms_inputs_args, **ms_inputs_kwargs) |
| 117 | + # print("ms:", ms_outputs) |
| 118 | + # print("pt:", pt_outputs) |
| 119 | + if outputs_map: |
| 120 | + pt_outputs_n = [] |
| 121 | + ms_outputs_n = [] |
| 122 | + for pt_key, ms_idx in outputs_map.items(): |
| 123 | + # print("===map", pt_key, ms_idx) |
| 124 | + pt_output = getattr(pt_outputs, pt_key) |
| 125 | + ms_output = ms_outputs[ms_idx] |
| 126 | + if isinstance(pt_output, (list, tuple)): |
| 127 | + pt_outputs_n += list(pt_output) |
| 128 | + ms_outputs_n += list(ms_output) |
| 129 | + else: |
| 130 | + pt_outputs_n.append(pt_output) |
| 131 | + ms_outputs_n.append(ms_output) |
| 132 | + diffs = compute_diffs(pt_outputs_n, ms_outputs_n) |
| 133 | + else: |
| 134 | + diffs = compute_diffs(pt_outputs, ms_outputs) |
| 135 | + |
| 136 | + THRESHOLD = DTYPE_AND_THRESHOLDS[ms_dtype] |
| 137 | + assert (np.array(diffs) < THRESHOLD).all(), ( |
| 138 | + f"ms_dtype: {ms_dtype}, pt_type:{pt_dtype}, " |
| 139 | + f"Outputs({np.array(diffs).tolist()}) has diff bigger than {THRESHOLD}" |
| 140 | + ) |
0 commit comments