|
| 1 | +# Copyright The FMS HF Tuning Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# Standard |
| 16 | +from functools import partial |
| 17 | + |
| 18 | +# Third Party |
| 19 | +from fms_acceleration.model_patcher import ( |
| 20 | + ModelPatcherRule, |
| 21 | + ModelPatcherTrigger, |
| 22 | + combine_functions, |
| 23 | + combine_triggers, |
| 24 | +) |
| 25 | + |
| 26 | +# Local |
| 27 | +from ..kernels.unsloth.cross_entropy_loss import ( |
| 28 | + FastCrossEntropyLoss, |
| 29 | + replace_custom_loss_when_triggered, |
| 30 | +) |
| 31 | +from ..kernels.unsloth.rms_layernorm import fast_rms_layernorm |
| 32 | +from ..kernels.unsloth.rope_embedding import fast_rope_embedding |
| 33 | +from .utils import ( |
| 34 | + KEY_O, |
| 35 | + KEY_QKV, |
| 36 | + build_lora_fused_ops, |
| 37 | + get_transformers_version, |
| 38 | + trigger_fused_ops, |
| 39 | +) |
| 40 | + |
| 41 | + |
| 42 | +def get_mp_rules(base_type: str): |
| 43 | + """ |
| 44 | + Function to access all patch rules in this module. |
| 45 | + If it is a forward_builder rule with `base_type` in |
| 46 | + its forward builder argument, wrap the forward_builder |
| 47 | + function as a partial function with the base_type argument |
| 48 | + """ |
| 49 | + try: |
| 50 | + # Third Party |
| 51 | + from transformers.models.granitemoehybrid.modeling_granitemoehybrid import ( # pylint: disable=import-outside-toplevel |
| 52 | + GraniteMoeHybridAttention, |
| 53 | + GraniteMoeHybridForCausalLM, |
| 54 | + GraniteMoeHybridRMSNorm, |
| 55 | + ) |
| 56 | + except ImportError: |
| 57 | + return [] |
| 58 | + |
| 59 | + return [ |
| 60 | + # TODO: have a generic version of this rule |
| 61 | + # - do regex on RMSNorm class name |
| 62 | + # - check on the tensors required for fast_rms_layernorm |
| 63 | + ModelPatcherRule( |
| 64 | + rule_id="granitemoehybrid-rms", |
| 65 | + trigger=ModelPatcherTrigger(check=GraniteMoeHybridRMSNorm), |
| 66 | + forward=fast_rms_layernorm, |
| 67 | + ), |
| 68 | + # TODO: have a generic version of this rule |
| 69 | + # - do regex on Attention class name |
| 70 | + # - have a set of qkv / o module names and check on that |
| 71 | + ModelPatcherRule( |
| 72 | + rule_id="granitemoehybrid-qkvo", |
| 73 | + trigger=combine_triggers( |
| 74 | + ModelPatcherTrigger( |
| 75 | + check=partial( |
| 76 | + trigger_fused_ops, |
| 77 | + attn_cls=GraniteMoeHybridAttention, |
| 78 | + submodule_names=["q_proj", "k_proj", "v_proj"], |
| 79 | + ) |
| 80 | + ), |
| 81 | + ModelPatcherTrigger( |
| 82 | + check=partial( |
| 83 | + trigger_fused_ops, |
| 84 | + attn_cls=GraniteMoeHybridAttention, |
| 85 | + submodule_names=["o_proj"], |
| 86 | + ) |
| 87 | + ), |
| 88 | + logic="OR", |
| 89 | + ), |
| 90 | + forward_builder=combine_functions( |
| 91 | + partial( |
| 92 | + build_lora_fused_ops, |
| 93 | + submodule_names=["q_proj", "k_proj", "v_proj"], |
| 94 | + fused_op=KEY_QKV, |
| 95 | + base_type=base_type, |
| 96 | + ), |
| 97 | + partial( |
| 98 | + build_lora_fused_ops, |
| 99 | + submodule_names=["o_proj"], |
| 100 | + fused_op=KEY_O, |
| 101 | + base_type=base_type, |
| 102 | + ), |
| 103 | + logic="APPEND", |
| 104 | + ), |
| 105 | + ), |
| 106 | + *[ |
| 107 | + ( |
| 108 | + ModelPatcherRule( |
| 109 | + rule_id="granitemoehybrid-custom-loss", |
| 110 | + trigger=ModelPatcherTrigger( |
| 111 | + check=replace_custom_loss_when_triggered( |
| 112 | + GraniteMoeHybridForCausalLM, |
| 113 | + custom_loss_type="granite-custom-loss", |
| 114 | + ) |
| 115 | + ), |
| 116 | + ) |
| 117 | + if get_transformers_version() >= "4.46" |
| 118 | + else ModelPatcherRule( |
| 119 | + rule_id="granitemoehybrid-cross-ent", |
| 120 | + import_and_maybe_reload=( |
| 121 | + "torch.nn.CrossEntropyLoss", |
| 122 | + FastCrossEntropyLoss, |
| 123 | + "transformers.models.granitemoehybrid.modeling_granitemoehybrid", |
| 124 | + ), |
| 125 | + ) |
| 126 | + ) |
| 127 | + ], |
| 128 | + # TODO: have a generic version of this rule |
| 129 | + # - get the module name |
| 130 | + # - check if "apply_rotary_pos_emb" exists |
| 131 | + # - patch |
| 132 | + ModelPatcherRule( |
| 133 | + rule_id="granitemoehybrid-rope", |
| 134 | + import_and_maybe_reload=( |
| 135 | + "transformers.models.granitemoehybrid.\ |
| 136 | + modeling_granitemoehybrid.apply_rotary_pos_emb", |
| 137 | + fast_rope_embedding, |
| 138 | + None, |
| 139 | + ), |
| 140 | + ), |
| 141 | + ] |
0 commit comments