# Initialize Otter
import otter
grader = otter.Notebook("quiz9-classes-practice.ipynb")

βŒ›οΈ Quiz 9 Practice - Classes#

This quiz will evaluate your mastery of using classes in Python. Functions provide a way to isolate code that you want to use repeatedly, and they allow you to pass in data to the code and get data back out of the code.

Entering Your Information for Credit#

To receive credit for assignments it is important we can identify your work from others. To do this we will ask you to enter your information in the following code block.

Before you begin#

Run the block of code at the top of the notebook that imports and sets up the autograder. This will allow you to check your work.

import pkg_resources
from subprocess import call
import sys

package_name = 'ENGR131_Util_2024'
version = '0.1.11'
package_version = f'{package_name}=={version}'

try:
    # Check if the package and version are installed
    pkg_resources.require(package_version)
    print(f'{package_version} is already installed.')
except pkg_resources.DistributionNotFound:
    # If not installed, install the package
    print(f'{package_version} not found. Installing...')
    call([sys.executable, '-m', 'pip', 'install', package_version])
except pkg_resources.VersionConflict:
    # If a different version is installed, you can choose to upgrade/downgrade
    installed_packages = {dist.key: dist.version for dist in pkg_resources.working_set}
    installed_version = installed_packages.get(package_name.lower())
    print(f'{package_name} {installed_version} is installed, but {version} is required.')
    # Optionally, upgrade or downgrade the package to the required version
    call([sys.executable, '-m', 'pip', 'install', '--upgrade', package_version])
# DO NOT MODIFY THIS CELL
from ENGR131_Util_2024 import cell_logger, StudentInfoForm, responses, upsert_to_json_file
# Register the log function to be called before any cell is executed
get_ipython().events.register('pre_run_cell', cell_logger)
responses["assignment"] = "quiz_9_practice"

StudentInfoForm(**responses)

Question: Machine Learning Model Resource Estimator#

Background#

In the field of machine learning and artificial intelligence, estimating the computational resources required for training a model is critical for efficiency and cost management. The total resource requirement includes both the static load (the base computational power and memory needed for the model architecture, datasets, and fixed algorithms) and the dynamic load (additional resources required for training iterations, data augmentation, and validation processes).

Objectives#

  1. Implement a ModelResourceEstimator class to model the resource requirements for training a machine learning model.

  2. Include methods within this class to calculate the static load, dynamic load, and total resource requirement.

  3. Demonstrate the use of class initialization, basic math calculations, and functions calling other functions.

Class to Implement#

Implement a class ModelResourceEstimator with the following properties and methods:

  • Methods:

    • __init__: Initializes a new ModelResourceEstimator instance with the properties.

      • Properties:

        • model_complexity (a measure of model complexity, e.g., the number of parameters)

        • data_size (in gigabytes, GB, representing the size of the dataset used for training)

        • iterations (the number of training iterations)

        • base_resource_per_iteration (in gigaflops per iteration, representing the base computational resource usage per training iteration)

        • memory_usage_static (in gigabytes, GB, representing the static memory usage by the model and dataset)

    • static_load: Calculates and returns the static resource load for training the model.

      • The static load includes the base static memory usage.

    • dynamic_load: Calculates and returns the dynamic resource load for the model based on the number of iterations and base resource per iteration.

      • The dynamic load is calculated as the product of the number of iterations and the base resource usage per iteration.

    • total_resource_requirement: Calculates and returns the total resource requirement by summing its static and dynamic loads.

      • The total resource requirement is the sum of the static and dynamic loads.

    • print_resource_requirements: Prints the static, dynamic, and total resource requirements in the following format:

      • β€œStatic Load: β€œ{static_load} GB\nDynamic Load: {dynamic_load} GFLOPs\nTotal Resource Requirement: {total_resource_requirement} (GB for static load and GFLOPs for dynamic load)” where the values are extracted from the object rounded to 2 decimal places.

      • Note: you must use a single print command, \n is used to create a new line in the print statement.

Instantiate a ModelResourceEstimator object with the following properties:

  • Model complexity: High (not directly quantified here, but influences resource estimation)

  • Dataset size: 10 GB

  • Number of training iterations: 1000

  • Base computational resource per iteration: 5 gigaflops

  • Static memory usage: 2 GB

# Your Class for a Machine Learning Model Resource Estimator goes Here
...

# Instantiate a ModelResourceEstimator object with the specified properties
...

# Print the static, dynamic, and total resource requirements
# you can uncomment this line for testing
# model_resource_estimator.print_resource_requirements()
grader.check("q1-ML-Resource")

Fin#