code: def test_gpt2_perplexity():
      gpt2 = models.get_model("gpt2").create_from_arg_string(
          "device=cpu"
      )
      test_string = "We study empirical scaling laws for language model performance on the cross-entropy loss."
      perplexity = gpt2.loglikelihood_rolling([(test_string,)])[0]
      tgt = sum(
          [
              -4.9599953,
              -8.069298,
              -8.308624,
              -10.178513,
              -8.906924,
              -1.9318912,
              -7.745445,
              -7.146077,
              -5.2072,
              -3.5882986,
              -1.9957212,
              -8.044922,
              -0.20841774,
              -5.1096807,
              -0.099879116,
              -8.888423,
              -4.6180487,
          ]
      )
      assert perplexity == pytest.approx(tgt, rel=1e-3)
  
      with mock.patch.object(
          models.gpt2.HFLM, "max_length", new_callable=mock.PropertyMock
      ) as mock_max_length:
          mock_max_length.return_value = 5
          gpt2 = models.get_model("gpt2").create_from_arg_string(
              "device=cpu"
          )
          perplexity = gpt2.loglikelihood_rolling([(test_string,)])[0]
      tgt = sum(
          [
              -4.96001,
              -8.069275,
              -8.308612,
              -10.178482,
              -8.90691,
              -4.037338,
              -8.09261,
              -11.662385,
              -10.206891,
              -4.425003,
              -2.2563353,
              -7.909143,
              -1.9304147,
              -7.3610134,
              -2.3120654,
              -7.3229,
              -2.1643813,
          ]
      )
      assert perplexity == pytest.approx(tgt, rel=1e-3)
  
file_path: lm-evaluation-harness/tests/test_models.py
imports: import hashlib
  import json
  import openai
  import os
  import pickle
  import pytest
  import unittest.mock
  import lm_eval.models
label_desc: Use image node_id 2 for calling a global function with example usage: test_gpt2_perplexity() without return types