ModifyCode

Contents

ModifyCode#

This tutorial provides some examples on how to modify a str code using our provided ModifyCode class. The ModifyCode class reveals how SecureEvaluator add wrappers, random seeds, replace div with protected div to the “algorithm to be evaluated”.

Tutorial#

[1]:
from llm4ad.base import ModifyCode
from llm4ad.base import TextFunctionProgramConverter
[2]:
example_program_str = '''\
import numpy as np
from typing import List

def example_function(arr: List | np.ndarray):
    """This is an example function."""
    max = np.max(arr)
    min = np.min(arr)
    result = max / min
    return result
'''

The “get_functions_called” function can extract the function name of the code.

[3]:
all_functions = ModifyCode.get_functions_name(example_program_str)
print(all_functions)
{'example_function'}

Rename the function. This can also be achieved by converting the text to a program, find the function in the program, and update its name attribute.

[4]:
function_name = TextFunctionProgramConverter.text_to_function(example_program_str).name
modified_program = ModifyCode.rename_function(example_program_str, function_name, 'ha_ha_ha_ha')
print(modified_program)
import numpy as np
from typing import List

def ha_ha_ha_ha(arr: List | np.ndarray):
    """This is an example function."""
    max = np.max(arr)
    min = np.min(arr)
    result = max / min
    return result

Add a numba.jit() wrapper to the function.

[5]:
function_name = TextFunctionProgramConverter.text_to_function(example_program_str).name
modified_program = ModifyCode.add_numba_decorator(example_program_str, function_name)
print(modified_program)
import numba
import numpy as np
from typing import List

@numba.jit(nopython=True)
def example_function(arr: List | np.ndarray):
    """This is an example function."""
    max = np.max(arr)
    min = np.min(arr)
    result = max / min
    return result

Set random seeds in the program.

[6]:
function_name = TextFunctionProgramConverter.text_to_function(example_program_str).name
modified_program = ModifyCode.add_np_random_seed_below_numpy_import(example_program_str, seed=2024)
print(modified_program)
import numpy as np
np.random.seed(2024)
from typing import List

def example_function(arr: List | np.ndarray):
    """This is an example function."""
    max = np.max(arr)
    min = np.min(arr)
    result = max / min
    return result

Import additional packages in the program.

[7]:
modified_function = ModifyCode.add_import_package_statement(example_program_str, package_name='pandas', as_name='pd')
print(modified_function)
import pandas as pd
import numpy as np
from typing import List

def example_function(arr: List | np.ndarray):
    """This is an example function."""
    max = np.max(arr)
    min = np.min(arr)
    result = max / min
    return result

Replace all normal div operation with the protected version (achieved using ast).

[8]:
modified_function = ModifyCode.replace_div_with_protected_div(example_program_str, delta=1e-4)
print(modified_function)
import numpy as np
from typing import List

def example_function(arr: List | np.ndarray):
    """This is an example function."""
    max = np.max(arr)
    min = np.min(arr)
    result = _protected_div(max, min)
    return result


def _protected_div(x, y, delta=0.0001):
    return x / (y + delta)

[ ]: