Run this model with HF Jobs
Run openbmb/MiniCPM-Reranker on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/openbmb/MiniCPM-Reranker. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/openbmb/MiniCPM-Reranker on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Instructions to use openbmb/MiniCPM-Reranker with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use openbmb/MiniCPM-Reranker with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="openbmb/MiniCPM-Reranker", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForSequenceClassification model = AutoModelForSequenceClassification.from_pretrained("openbmb/MiniCPM-Reranker", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Update modeling_minicpm.py
Browse files26 unmodified lines272829303132331 unmodified line353637383940279 unmodified lines320321322323324325326327328720 unmodified lines1049105010511052105310541055105610571058105910601061106210631064106553 unmodified lines1119112011211122112311241125209 unmodified lines13351336133713381339134026 unmodified linesimport torch.utils.checkpointfrom torch import nnfrom torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELossfrom transformers.activations import ACT2FNfrom transformers.cache_utils import Cache, DynamicCachefrom transformers.modeling_attn_mask_utils import (1 unmodified line_prepare_4d_attention_mask,_prepare_4d_causal_attention_mask,_prepare_4d_causal_attention_mask_for_sdpa,)from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast, SequenceClassifierOutputWithPastfrom transformers.modeling_utils import PreTrainedModel279 unmodified linesself.rope_theta = config.rope_thetaself.is_causal = config.is_causallogger.info(f"self.is_causal = {self.is_causal}")if (self.head_dim * self.num_heads) != self.hidden_size:raise ValueError(720 unmodified lineselif self._use_sdpa and not output_attentions:# output_attentions=True can not be supported when using SDPA, and we fall back on# the manual implementation that requires a 4D causal mask in all cases.attention_mask = _prepare_4d_causal_attention_mask_for_sdpa(attention_mask,(batch_size, seq_length),inputs_embeds,past_key_values_length,)else:# 4d mask is passed through the layersattention_mask = _prepare_4d_causal_attention_mask(attention_mask, (batch_size, seq_length), inputs_embeds, past_key_values_length)# embed positionshidden_states = inputs_embeds53 unmodified linesattentions=all_self_attns,)class MiniCPMForCausalLM(MiniCPMPreTrainedModel):_tied_weights_keys = ["lm_head.weight"]209 unmodified linesreturn response, history@add_start_docstrings("""The MiniCPM Model transformer with a sequence classification head on top (linear layer).26 unmodified lines272829303132331 unmodified line35363738394041279 unmodified lines321322323324325326720 unmodified lines1047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107553 unmodified lines112911301131113211331134209 unmodified lines1344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137526 unmodified linesimport torch.utils.checkpointfrom torch import nnfrom torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELossfrom transformers import LlamaTokenizerfrom transformers.activations import ACT2FNfrom transformers.cache_utils import Cache, DynamicCachefrom transformers.modeling_attn_mask_utils import (1 unmodified line_prepare_4d_attention_mask,_prepare_4d_causal_attention_mask,_prepare_4d_causal_attention_mask_for_sdpa,_prepare_4d_attention_mask_for_sdpa,)from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast, SequenceClassifierOutputWithPastfrom transformers.modeling_utils import PreTrainedModel279 unmodified linesself.rope_theta = config.rope_thetaself.is_causal = config.is_causalif (self.head_dim * self.num_heads) != self.hidden_size:raise ValueError(720 unmodified lineselif self._use_sdpa and not output_attentions:# output_attentions=True can not be supported when using SDPA, and we fall back on# the manual implementation that requires a 4D causal mask in all cases.if self.is_causal:attention_mask = _prepare_4d_causal_attention_mask_for_sdpa (attention_mask,(batch_size, seq_length),inputs_embeds,past_key_values_length,)else:attention_mask = _prepare_4d_attention_mask_for_sdpa(attention_mask,inputs_embeds.dtype,)else:# 4d mask is passed through the layersif self.is_causal:attention_mask = _prepare_4d_causal_attention_mask (attention_mask, (batch_size, seq_length), inputs_embeds, past_key_values_length)else:attention_mask = _prepare_4d_attention_mask(attention_mask,inputs_embeds.dtype,)# embed positionshidden_states = inputs_embeds53 unmodified linesattentions=all_self_attns,)class MiniCPMForCausalLM(MiniCPMPreTrainedModel):_tied_weights_keys = ["lm_head.weight"]209 unmodified linesreturn response, historyclass MiniCPMRerankerLLamaTokenizer(LlamaTokenizer):def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1 = None):"""- single sequence: `<s> X </s>`- pair of sequences: `<s> A </s> B`Args:token_ids_0 (`List[int]`):List of IDs to which the special tokens will be added.token_ids_1 (`List[int]`, *optional*):Optional second list of IDs for sequence pairs.Returns:`List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens."""if token_ids_1 is None:return super().build_inputs_with_special_tokens(token_ids_0)bos = [self.bos_token_id]sep = [self.eos_token_id]return bos + token_ids_0 + sep + token_ids_1@add_start_docstrings("""The MiniCPM Model transformer with a sequence classification head on top (linear layer).
modeling_minicpm.py+49-14