Home / Function/ generate_inputs_for_model() — pytorch Function Reference

generate_inputs_for_model() — pytorch Function Reference

Architecture documentation for the generate_inputs_for_model() function in huggingface.py from the pytorch codebase.

Entity Profile

Dependency Diagram

graph TD
  179ecd7f_27e8_48db_95f4_85c2cda45c18["generate_inputs_for_model()"]
  f731aa1a_ba23_ab06_f8e5_e87d6a842749["load_model()"]
  f731aa1a_ba23_ab06_f8e5_e87d6a842749 -->|calls| 179ecd7f_27e8_48db_95f4_85c2cda45c18
  19c1813c_4f26_3218_718c_6182fe70675d["get_sequence_length()"]
  179ecd7f_27e8_48db_95f4_85c2cda45c18 -->|calls| 19c1813c_4f26_3218_718c_6182fe70675d
  99522623_9bfd_bf9f_64dc_57a37f41f30a["rand_int_tensor()"]
  179ecd7f_27e8_48db_95f4_85c2cda45c18 -->|calls| 99522623_9bfd_bf9f_64dc_57a37f41f30a
  style 179ecd7f_27e8_48db_95f4_85c2cda45c18 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

benchmarks/dynamo/huggingface.py lines 190–292

def generate_inputs_for_model(
    model_cls, model, model_name, bs, device, include_loss_args=False
):
    # TODO - Check if following values are representative
    num_choices = 3
    num_visual_features = 42
    seq_length = get_sequence_length(model_cls, model_name)
    vocab_size = model.config.vocab_size

    if model_name.startswith("Wav2Vec2"):
        # TODO: If we add more input_values style models, try to work this
        # into the overall control flow
        target_length = 100
        return {
            "input_values": torch.randn((bs, seq_length), device=device),
            # Added because that's what the example training script has
            "attention_mask": rand_int_tensor(device, 0, 2, (bs, seq_length)),
            "labels": rand_int_tensor(device, 0, vocab_size, (bs, target_length)),
        }

    if model_name.endswith("MultipleChoice"):
        input = rand_int_tensor(device, 0, vocab_size, (bs, num_choices, seq_length))
    elif model_name.startswith("Roberta"):
        input = rand_int_tensor(device, 0, 1, (bs, seq_length))
    else:
        input = rand_int_tensor(device, 0, vocab_size, (bs, seq_length))

    if "Bart" in model_name:
        input[:, -1] = model.config.eos_token_id

    input_dict = {"input_ids": input}

    if model_name.startswith(("T5", "M2M100", "MT5")) or model_cls in [
        BlenderbotModel,
        BlenderbotSmallModel,
        BlenderbotForConditionalGeneration,
        PegasusModel,
        MarianModel,
        MarianMTModel,
    ]:
        input_dict["decoder_input_ids"] = input

    if model_name.startswith("Lxmert"):
        visual_feat_dim, visual_pos_dim = (
            model.config.visual_feat_dim,
            model.config.visual_pos_dim,
        )
        input_dict["visual_feats"] = torch.randn(
            bs, num_visual_features, visual_feat_dim
        )
        input_dict["visual_pos"] = torch.randn(bs, num_visual_features, visual_pos_dim)

    if include_loss_args:
        if model_name.endswith("PreTraining"):
            if model_cls in [ElectraForPreTraining, LxmertForPreTraining]:
                input_dict["labels"] = rand_int_tensor(device, 0, 1, (bs, seq_length))
            else:
                label_name = (
                    "sentence_order_label"
                    if model_cls in [AlbertForPreTraining]
                    else "next_sentence_label"
                )
                input_dict["labels"] = (
                    rand_int_tensor(device, 0, vocab_size, (bs, seq_length)),
                )
                input_dict[label_name] = rand_int_tensor(device, 0, 1, (bs,))
        elif model_name.endswith("QuestionAnswering"):
            input_dict["start_positions"] = rand_int_tensor(
                device, 0, seq_length, (bs,)
            )
            input_dict["end_positions"] = rand_int_tensor(device, 0, seq_length, (bs,))
        elif model_name.endswith(
            ("MaskedLM", "HeadModel", "CausalLM", "DoubleHeadsModel")
        ):
            input_dict["labels"] = rand_int_tensor(
                device, 0, vocab_size, (bs, seq_length)
            )
        elif model_name.endswith("TokenClassification"):
            input_dict["labels"] = rand_int_tensor(
                device, 0, model.config.num_labels - 1, (bs, seq_length)
            )
        elif model_name.endswith("MultipleChoice"):
            input_dict["labels"] = rand_int_tensor(device, 0, num_choices, (bs,))
        elif model_name.endswith("SequenceClassification"):
            input_dict["labels"] = rand_int_tensor(
                device, 0, model.config.num_labels - 1, (bs,)
            )
        elif model_name.endswith("NextSentencePrediction"):
            input_dict["labels"] = rand_int_tensor(device, 0, 1, (bs,))
        elif model_name.endswith("ForConditionalGeneration"):
            input_dict["labels"] = rand_int_tensor(
                device, 0, vocab_size - 1, (bs, seq_length)
            )
        elif model_name in EXTRA_MODELS:
            input_dict["labels"] = rand_int_tensor(
                device, 0, vocab_size, (bs, seq_length)
            )
        else:
            raise NotImplementedError(
                f"Class {model_name} unsupported for training test "
            )

    return input_dict

Subdomains

Called By

Frequently Asked Questions

What does generate_inputs_for_model() do?
generate_inputs_for_model() is a function in the pytorch codebase.
What does generate_inputs_for_model() call?
generate_inputs_for_model() calls 2 function(s): get_sequence_length, rand_int_tensor.
What calls generate_inputs_for_model()?
generate_inputs_for_model() is called by 1 function(s): load_model.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free